package FarmView;

use strict;
use CGI qw(:standard);


use Athena::WebAPI::HTML;
use Athena::WebAPI::Form;
use Athena::WebAPI::Table;
use Athena::WebAPI::View;

use Athena::NavHeaderView;
use Athena::CellFormat qw(:all);

use base ("View");

sub new {

    # Get the class name
    my $stInvocant = shift(@_);
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    my %hParams = ( @_ );
    $rSelf->{params} = \%hParams;
    $rSelf->cellFormatInit();
    return $rSelf;
}

sub draw {
  my $rSelf = shift(@_);
  my $rParams = $rSelf->{params};

  my $rTable = Table->new(%{$rSelf->{params}->{table_style}});
  $rSelf->buildTable($rTable);

  my $rForm = Form->new(-method=>'post', -name=>'myForm');
  $rForm->addChild($rTable);
  $rForm->addHiddenFields($rParams->{'hidden_fields'});

  my $rHTML  = HTML->new();
  $rHTML->setBodyAttr(%{$rSelf->{params}->{body_style}});
  $rHTML->addChild($rForm);

  return $rHTML->draw();
}

sub buildTable {
   my $rSelf = shift(@_);
   my $rTable = shift(@_);
   my $iCurRow = 0;
   $rTable->addChild($iCurRow++, 0, NavHeaderView->new($rSelf->{params}));
   my $stTitle = "View Farms";
   if ($rSelf->{params}->{action_name} eq "UpdateFarmAction") {
     $stTitle .= ' <font color="#800000">Update Mode</font>';
   }
   $rTable->addChild($iCurRow++, 0, $stTitle,-bgcolor=>"#ccccdd");
   my $rFarmTable = Table->new(-border=>'1', -cellpadding=>'3');
   my @arCols = ('idx', 'name', 'on', 'allow', 'recover', 'expect', 'chosen', 'smartfarmable');
   my $iFCurRow = 0;
   my $iFCurCol = 0;

   my $stColName;
   foreach $stColName (@arCols) {
     $rFarmTable->addChild($iFCurRow, $iFCurCol++, $stColName,
			   -bgcolor=>$rSelf->colColor());
   }

   my $rFarms = $rSelf->{params}->{farms};
   my $rPhyss = $rSelf->{params}->{physs};
   my $key;
   foreach $key (sort(keys(%{$rFarms->{table}}))) {
     # resets all color switches
     #$rSelf->cellColor(1);
     $rSelf->colColor(1);
     #$rSelf->colTextColor(1,1);
     $iFCurRow++;
     $iFCurCol = 0;

     foreach $stColName (@arCols) {
       my $stColEl;
       if ($stColName eq "idx") {
	 $stColEl = "[" . $key . "]";
       }
       elsif ($stColName eq "on") {
	 if ($rSelf->{params}->{action_name} ne "UpdateFarmAction") {
	   $stColEl = $rFarms->int_to_yn($rFarms->{table}->{$key}->{$stColName});
	 }
	 else {
	   $stColEl = checkbox(-name=>"idx.".$key.".".$stColName,
			       -checked=>$rFarms->{table}->{$key}->{$stColName},
			       -value=>'1',
			       -label=>'',
			       -onClick=>"document.myForm.need_update.value = \"1\"");
	 }
       }
       elsif ($stColName eq "expect") {
	 $stColEl = $rFarms->int_to_yn($rFarms->{table}->{$key}->{$stColName});
       }
       elsif ($stColName eq "chosen") {
	 $stColEl = $rPhyss->{table}->{ $rFarms->{table}->{$key}->{$stColName} }->{host};
       }
       elsif ( $stColName eq "smartfarmable") {
	 $stColEl = $rFarms->int_to_yn($rFarms->{table}->{$key}->{$stColName});
       else {
	 $stColEl = " " . $rFarms->{table}->{$key}->{$stColName} . " ";
       }
       $rFarmTable->addChild($iFCurRow, $iFCurCol++, $stColEl, 
			     -bgcolor=>$rSelf->colColor);
     }
     $iFCurRow++;
     $rFarmTable->addChild($iFCurRow, 0, "algorithm", -colspan=>2, 
			   -valign=>'top', -align=>'right');
     $rFarmTable->addChild($iFCurRow, 2, $rFarms->{table}->{$key}->{algodef},
			   -colspan=>(scalar(@arCols) - 2),
			   -bgcolor=>"#eeeeee");
     $iFCurRow++;
     $rFarmTable->addChild($iFCurRow, 0, "members", -colspan=>2, 
			   -valign=>'top', -align=>'right');
     my @arMembers = split(/,/, $rFarms->{table}->{$key}->{members});
     my $iMember;
     my $stMembers;
     foreach $iMember (@arMembers) {
       if (scalar($iMember) > -1) {
	 if ($rPhyss->{table}->{$iMember}->{on}) {
	   $stMembers .= '(on/';
	 }
	 else {
	   $stMembers .= '(<font color="#800000">off</font>/';
	 }
	 
	 if ($rPhyss->{table}->{$iMember}->{up}) {
	   $stMembers .= 'up/';
	 }
	 else {
	   $stMembers .= '<font color="#800000">down</font>/';
	 }
	 
	 if ($rPhyss->{table}->{$iMember}->{in}) {
	   $stMembers .= 'in) ';
	 }
	 else {
	   $stMembers .= '<font color="#800000">out</font>) ';
	 }
	 
	 $stMembers .= $rPhyss->{table}->{$iMember}->{host};
	 $stMembers .= "<br>\n";
       }
     }
     $rFarmTable->addChild($iFCurRow, 2, $stMembers, 
			   -colspan=>(scalar(@arCols) - 2),
			   -bgcolor=>"#eeeeee");
   }
   $rTable->addChild($iCurRow++, 0, $rFarmTable);

   $rTable->addChild($iCurRow++, 0, submit(-name=>'send'), 
		     -bgcolor=>'#ddcccc');
   $rTable->addChild($iCurRow++, 0, NavFooterView->new($rSelf->{params}));
}

1;
